package com.xiaoxiao.springbootstudy.controller;

import com.xiaoxiao.springbootstudy.pojo.BuyList;
import com.xiaoxiao.springbootstudy.pojo.InStore;
import com.xiaoxiao.springbootstudy.service.InStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
public class InStoreController {
    @Autowired
    private InStoreService inStoreService;

    @PostMapping("/buyInStore")
    public Map<String, Object> buyInStore(@RequestBody BuyList buyList) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 400);
        map.put("msg", "操作失败");

        try {
            inStoreService.saveBuyOrderInStoreService(buyList);

            map.put("code", 200);
            map.put("msg", "采购成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    @GetMapping("/queryInList")
    public Map<String, Object> queryInStoreList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        return inStoreService.queryInStoreListService(pageNum, pageSize);
    }

    @PostMapping("/updateInStore/{id}")
    public Map<String, Object> updateInStore(@PathVariable Integer id) {

        Map<String, Object> result = new HashMap<>();
        result.put("code", "操作失败");
        result.put("code", 400);
        try {
            InStore inStore = new InStore();
            inStore.setInsId(id);
            inStore.setIsIn("0");
            inStoreService.updateById(inStore);

            result.put("code", 200);
            result.put("msg", "入库单确认成功");
        } catch (Exception ex) {
            ex.printStackTrace();

        }
        return result;
    }
}
